package com.candy.service.impl;

import com.candy.bean.UserInfo;
import com.candy.dao.UserInfoMapper;
import com.candy.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
public class UserInfoServiceImpl implements UserinfoService {
    @Autowired
    UserInfoMapper userInfoMapper;

    /**
     * 查询未删除状态下的用户信息
     * @param userInfo
     * @return
     */
    @Override
    public List<UserInfo> getAllInfoes(UserInfo userInfo) {
        List<UserInfo> userInfos=userInfoMapper.selectByParams(userInfo);
        return userInfos;
    }

    /**
     * 新增或修改操作；根据主键id是否为空判断是新增还是编辑
     * @param userInfo
     * @return
     */
    @Override
    public Integer addOrUpdateUser(@RequestBody UserInfo userInfo) {
        Integer id=userInfo.getId();
        Integer num;
        if(id==null||id.equals("")){
            num=userInfoMapper.insertSelective(userInfo);
        }else{
            num=userInfoMapper.updateByPrimaryKeySelective(userInfo);
        }
        return num;
    }

    /**
     * 删除操作：采用逻辑删除实现此目的。通过改变isdeleted的值来修改用户状态
     * @param userInfo
     * @return
     */
    @Override
    public Integer deleteUser(UserInfo userInfo) {
        userInfo.setIsdelete(1);
        Integer num=userInfoMapper.updateByPrimaryKeySelective(userInfo);
        return num;
    }
}
